package com.shoeshopping.service.zx;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shoeshopping.dao.BaseDao;
import com.shoeshopping.entity.DeliveryAddress;
import com.shoeshopping.entity.Order;
import com.shoeshopping.entity.User;
import com.shoeshopping.formbean.zx.UserAdminForm;
import com.shoeshopping.utils.zx.EasyUIResult;
import com.shoeshopping.vo.zx.VoDeliveryAddress;
import com.shoeshopping.vo.zx.VoOrderAdmin;
import com.shoeshopping.vo.zx.VoUserAdmin;
import com.shoeshopping.vo.zx.VoUserAdminWithPassword;

@Service("userAdminService")
@Transactional
public class UserAdminServiceImpl implements UserAdminService {

	@Resource
	BaseDao baseDao;
	
	@Override
	public EasyUIResult list(UserAdminForm dataform) {
		String hql = "from User as u";
		List allList = baseDao.find(hql,null);
		
		List list = baseDao.findWithPageAndSort(hql, null, 
				dataform.getPage(), dataform.getRows(),
				dataform.getSort(), dataform.getOrder());
		
		List voList = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			User user = (User) list.get(i);
			VoUserAdmin userAdmin = new VoUserAdmin(user);
			
			voList.add(userAdmin);
		}
		
		return new EasyUIResult((long) allList.size(), voList);
	}

	@Override
	public EasyUIResult listAddress(UserAdminForm dataform) {
		if(dataform.getId() != null && dataform.getId().length() > 0){
			String hql = "from User as u where id=?";
			List parma = new ArrayList();
			parma.add(dataform.getId());
			
			User user = (User) baseDao.find(hql,parma).get(0);
			
			List addressList = user.getAddresses();
			
			
			List voList = new ArrayList();
			for (int i = 0; i < addressList.size(); i++) {
				DeliveryAddress deliveryAddress = (DeliveryAddress) addressList.get(i);
				VoDeliveryAddress voDeliveryAddress = new VoDeliveryAddress(deliveryAddress);
				
				voList.add(voDeliveryAddress);
			}
			
			return new EasyUIResult((long) 0, voList);
		}
		return null;
	}

	@Override
	public void delete(UserAdminForm dataform) {
		User user = new User();
		user.setId(dataform.getId());
		baseDao.delete(user);
	}

	@Override
	public boolean add(UserAdminForm dataform) {
		User user = new User();
		if(dataform.getId() != null && dataform.getId().length() > 0){
			user.setId(dataform.getId());
		}
		user.setUsername(dataform.getUsername().trim());
		user.setPassword(dataform.getPassword().trim());
		user.setEmail(dataform.getEmail().trim());
		user.setBalance(dataform.getBalance());
		baseDao.save(user);
		
		return true;
	}

	@Override
	public VoUserAdminWithPassword edit(UserAdminForm dataform) {
		String hql = "from User as u where u.id=?";
		List parma = new ArrayList();
		parma.add(dataform.getId());
		User user = (User) baseDao.find(hql, parma).get(0);
		VoUserAdminWithPassword voUserAdminWithPassword = new VoUserAdminWithPassword(user); 
		return voUserAdminWithPassword;
	}

}
